package Exercise2;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class StudentTest {
    public static void main(String[] args) {
        Student[] arr = new Student[3];
        enum Status {
            Exit,
            OK
        }
        Status s1 = Status.OK;
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        ArrayList<User> UserName = new ArrayList<>();

        while (flag) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("选择你的操作" + "\n" + "1.登录" + " " + "2.注册" + " " + "3.忘记密码" + " " + "0.退出");
            int newa = sc.nextInt();
            switch (newa) {
                case 1: {
                    System.out.println("请输入用户名：");
                    String username = sc.next();
                    System.out.println("请输入密码");
                    String password = sc.next();
                    String testify1 = testify();
                    System.out.println("验证码为" + testify1);
                    System.out.println("请输入验证码");
                    String testify2 = sc.next();
                    if (Load(UserName, username, password, testify1, testify2)) {
                        flag = false;
                        break;
                    } else {
                        System.out.println("重新选择");
                        break;
                    }
                }
                case 2: {
                    while (true) {
                        System.out.println("输入用户名：");
                        String username = sc.next();
                        System.out.println("输入密码：");
                        String password1 = sc.next();
                        System.out.println("再次输入密码：");
                        String password2 = sc.next();
                        System.out.println("请输入身份证：");
                        String id = sc.next();
                        System.out.println("请输入手机号：");
                        String phoneNumber = sc.next();
                        if (Register(UserName, username, password1, password2, id, phoneNumber)) {
                            System.out.println("注册成功");
                            break;
                        }
                    }
                    break;
                }
                case 3: {
                    System.out.println("请输入用户名");
                    String username = sc.next();
                    System.out.println("请输入身份证");
                    String id=sc.next();
                    System.out.println("请输入电话号码");
                    String PhoneNum=sc.next();
                    if(ForgetPassword(UserName,username,id,PhoneNum)){
                        break;
                    }else{
                        System.out.println("修改失败");
                        break;
                    }
                }
                case 0: {
                    System.out.println("退出成功");
                    flag = false;
                    break;
                }
                default:
                    System.out.println("输入错误");
                    break;
            }
        }


        do {
            System.out.println("1.添加学生信息" + "  " + "2.删除学生信息" + "  " + "3.查询学生信息" + " " + "4.修改学生信息" + " " + "0.退出");
            int a = sc.nextInt();
            switch (a) {
                case 1: {
                    //数组没有满
                    if (Judge(arr) != arr.length) {
                        for (int i = 0; i < arr.length; i++) {
                            if (arr[i] == null) {
                                arr[i] = new Student();
                                System.out.println("请输入要添加学生的学号");
                                String id = sc.next();
                                if (JudgeOnly(arr, id)) {
                                    arr[i].setId(id);
                                    System.out.println("请输入要添加学生的名字");
                                    String name = sc.next();
                                    arr[i].setName(name);
                                    System.out.println("请输入要添加学生的年龄");
                                    int age = sc.nextInt();
                                    arr[i].setAge(age);
                                    System.out.println("success");
                                    Print(arr);
                                    break;
                                } else {
                                    System.out.println("已存在");
                                    break;
                                }
                            }
                        }
                    }
                    //数组满了的话
                    else {
                        arr = Creat(arr);
                        System.out.println("请输入要添加学生的学号");
                        String id = sc.next();
                        arr[arr.length - 1].setId(id);
                        System.out.println("请输入要添加学生的名字");
                        String name = sc.next();
                        arr[arr.length - 1].setName(name);
                        System.out.println("请输入要添加学生的年龄");
                        int age = sc.nextInt();
                        arr[arr.length - 1].setAge(age);
                        System.out.println("success");
                        Print(arr);
                        break;
                    }
                    break;
                }
                case 2: {
                    System.out.println("输入要删除学生的学号");
                    String input = sc.next();
                    boolean flag1 = true;
                    for (int i = 0; i < arr.length; i++) {
                        if (arr[i] != null) {
                            if (arr[i].getId().equals(input)) {
                                arr[i] = null;
                                System.out.println("删除成功");
                                flag1 = false;
                                Print(arr);
                                break;
                            }
                        }
                    }
                    if (flag1) {
                        System.out.println("不存在的学生");
                    }
                    break;
                }
                case 3: {
                    System.out.println("请输入要查询的学号");
                    String input = sc.next();
                    boolean flag1 = true;
                    for (int i = 0; i < arr.length; i++) {
                        if (arr[i] != null) {
                            if (arr[i].getId().equals(input)) {
                                System.out.println("找到了，该学生信息：");
                                System.out.println("学号为" + arr[i].getId() + ",姓名为" + arr[i].getName() + ",年龄为" + arr[i].getAge());
                                flag1 = false;
                                break;
                            }
                        }
                    }
                    if (flag1) {
                        System.out.println("不存在的学生");
                    }
                    break;
                }
                case 4: {
                    System.out.println("请输入要修改的学生的学号");
                    String input = sc.next();
                    boolean flag1 = true;
                    for (int i = 0; i < arr.length; i++) {
                        if (arr[i] != null) {
                            if (arr[i].getId().equals(input)) {
                                System.out.println("输入修改后的学号");
                                String id = sc.next();
                                arr[i].setId(id);
                                System.out.println("输入修改后的姓名");
                                String name = sc.next();
                                arr[i].setName(name);
                                System.out.println("输入修改后的年龄");
                                int age = sc.nextInt();
                                arr[i].setAge(age);
                                System.out.println("success");
                                flag1 = false;
                                Print(arr);
                                break;
                            }
                        }
                    }
                    if (flag1) {
                        System.out.println("不存在的学生");
                    }
                    break;
                }
                case 0:
                    s1 = Status.Exit;
            }
        } while (s1 != Status.Exit);
    }

    public static int Judge(Student[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                count++;
            }
        }
        return count;
    }

    public static Student[] Creat(Student[] arr) {
        Student[] arr1 = new Student[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            arr1[i] = arr[i];
        }
        arr1[arr1.length - 1] = new Student();
        return arr1;
    }

    public static void Print(Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                System.out.println("学号为" + arr[i].getId() + ",姓名为" + arr[i].getName() + ",年龄为" + arr[i].getAge());
            }
        }
    }

    public static boolean JudgeOnly(Student[] arr, String input) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                if (arr[i].getId() != null) {
                    if (arr[i].getId().equals(input)) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public static boolean Register(ArrayList<User> UserName, String username, String password1, String password2, String id, String phoneNumber) {
        //判断用户名
        if (username.length() < 3 || username.length() > 15) {
            System.out.println("非法长度");
            return false;
        } else {
            int count1 = 0;
            int count2 = 0;
            for (int i = 0; i < username.length(); i++) {
                char c = username.charAt(i);
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                    count1++;
                } else if (c >= '0' && c <= '9') {
                    count2++;
                }
            }
            if (count1 + count2 != username.length() || count1 == 0) {
                System.out.println("只能用字母和数字的组合！");
                return false;
            }
            for (int i = 0; i < UserName.size(); i++) {
                if (UserName.get(i) != null) {
                    if (username.equals(UserName.get(i).getUsername())) {
                        System.out.println("用户名已存在");
                        return false;
                    }
                }
            }
        }
        System.out.println("用户名合法");
        //判断密码
        if (password1.equals(password2)) {
            System.out.println("输入一致");
        } else {
            System.out.println("两次输入不一致");
            return false;
        }
        //判断身份证
        if (id.length() != 18) {
            System.out.println("输入18位身份证号");
            return false;
        } else {
            if (id.charAt(0) == 0) {
                System.out.println("身份证首位不能是0");
                return false;
            }
            for (int i = 0; i < id.length(); i++) {
                if (i < 17) {
                    if (id.charAt(i) < '0' && id.charAt(i) > '9') {
                        System.out.println("身份证输入不正确");
                        return false;
                    }
                } else {
                    if ((id.charAt(i) >= '0' && id.charAt(i) <= '9') || id.charAt(i) == 'x' || id.charAt(i) == 'X') {
                        System.out.println("身份证输入正确");
                    } else {
                        System.out.println("最后一位不合法");
                        return false;
                    }
                }
            }
        }

        //判断手机号
        if (phoneNumber.length() != 11) {
            System.out.println("非法手机号长度");
            return false;
        } else {
            if (phoneNumber.charAt(0) == 0) {
                System.out.println("手机号不能以0开头");
                return false;
            } else {
                for (int i = 0; i < phoneNumber.length(); i++) {
                    if (phoneNumber.charAt(i) < '0' && phoneNumber.charAt(i) > '9') {
                        System.out.println("非法手机号");
                        return false;
                    }
                }
            }
        }
        System.out.println("手机号正确");
        User newuser = new User(username, password1, id, phoneNumber);
        UserName.add(newuser);
        return true;

    }

    public static boolean Load(ArrayList<User> list, String username, String password, String testify1, String testify2) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUsername().equals(username)) {
                //用户名存在
                if (testify1.equals(testify2)) {
                    if (password.equals(list.get(i).getPassword())) {
                        System.out.println("登陆成功");
                        return true;
                    } else {
                        System.out.println("密码错误");
                        return false;
                    }
                } else {
                    System.out.println("验证码输入错误");
                    return false;
                }

            }
        }
        System.out.println("用户名未注册，请先注册后再登录");
        return false;
    }

    public static String testify() {
        Random r = new Random();
        char[] re = new char[5];
        for (int i = 0; i < re.length; i++) {
            char a = (char) (r.nextInt(26) + 65);
            char b = (char) (r.nextInt(26) + 97);
            int c = r.nextInt(2);
            if (c == 1) {
                re[i] = a;
            } else {
                re[i] = b;
            }
        }
        char d = (char) (r.nextInt(10) + 48);
        int e = r.nextInt(5);
        re[e] = d;
        String result = "";
        for (int i = 0; i < re.length; i++) {
            result += re[i];
        }
        return result;
    }

    public static boolean ForgetPassword(ArrayList<User> list, String username, String id, String PhoneNum) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUsername().equals(username)) {
                if (list.get(i).getId().equals(id)) {
                    if (list.get(i).getPhoneNumber().equals(PhoneNum)) {
                        System.out.println("请输入新的密码");
                        String newPassword = sc.next();
                        list.get(i).setPassword(newPassword);
                        System.out.println("修改成功");
                        return true;
                    } else {
                        System.out.println("手机号输入错误");
                        return false;
                    }
                } else {
                    System.out.println("身份证号错误");
                    return false;
                }
            }
        }
        System.out.println("用户名未注册");
        return false;
    }

}
